<?php
// +----------------------------------------------------------------------
// | 产品渠道配置
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/07/29
// +----------------------------------------------------------------------
include("/var/www/html/new/com.inc.php");
include('/var/www/html/new/d/class/MemberAccount.class.php');
include('/var/www/html/new/d/class/Alliance.class.php');
include('/var/www/html/new/d/class/Member.php');
include('/var/www/html/new/d/class/abc/Syslog.class.php');
include('/var/www/html/new/d/class/abc/Member.class.php');


if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');

$memberSID  = $_SESSION['sid'];


$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$action = trim($_REQUEST['action']);


switch($action){
		

	
	// 渠道设置
	case 'setChannels':
		
		
		$pids_a = array_values($_REQUEST['pid']);
		$pids_s = implode(',', $pids_a);
		
		$runtime = date('Y-m-d H:i:s');
		$channel_list = '';
		if(count($_REQUEST['channels'])) $channel_list = implode(',', array_values($_REQUEST['channels']));

		// 获取已存在记录
		$chnnel_set = array();
		$sql = "select id,aid,pid,status from pft_sale_channel where fid=$memberSID and pid in($pids_s)";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $chnnel_set[$row['pid'].'_'.$row['aid']] = $row;
		
		$ins   = "insert into pft_sale_channel (fid,aid,pid,lid,channel,rectime) values ";
		foreach($_REQUEST['pid'] as $key=>$pid){
			$supplier = $_REQUEST['aid'][$key];
			if(isset($chnnel_set[$key])){// 供应商供应的产品存在记录
				$uCids[] = $chnnel_set[$key]['id'];
			}else{
				$lid = $_REQUEST['lid'][$key];
				$ins.= "($memberSID,$supplier,$pid,$lid,'$channel_list','$runtime'),";
			}
		}		
		
		// 有新增需求
		$ins = rtrim($ins, ',');
		if(strrpos($ins, $memberSID)!==false){
			$GLOBALS['le']->query($ins);// echo $ins;// 
		}

		// 有更新需求
		$uSize = count($uCids);
		if($uSize>0){
			$cid = implode(',', $uCids);
			$sql = "update pft_sale_channel set channel='$channel_list',status=0 where id in ($cid) and fid=$memberSID limit $uSize";
			$GLOBALS['le']->query($sql);
		}
		
		$channels_t = array(1=>'微信', 2=>'二级店铺', 3=>'自助机', 4=>'会员卡');
		// 渠道文本处理
		$channel_t = '';
		if(count($_REQUEST['channels'])){
			foreach($_REQUEST['channels'] as $chlRow) $channel_t .= $channels_t[$chlRow].',';
			$channel_t = rtrim($channel_t, ',');
		}
		
		

		exit('{"status":"success","msg":"设置成功","channel":"'.$channel_t.'"}');
		break;
	
	// 设置排序
	case 'setSort':
		$aid  = $_REQUEST['aid'] + 0;
		$lid  = $_REQUEST['lid'] + 0;
		// $pid  = $_REQUEST['pid'] + 0;
		$sort = $_REQUEST['sort'] + 0;
		if($aid==0 || $lid==0) exit('{"status":"fail","msg":"参数错误"}');
		// $sql = "select id,aid,pid,status,channel from pft_sale_channel where fid=$memberSID and pid=$pid and aid=$aid limit 1";
		$sql = "select id,aid,pid,status,channel from pft_sale_channel where fid=$memberSID and lid=$lid and aid=$aid and channel<>'' limit 1";
		$GLOBALS['le']->query($sql);
		if(!$GLOBALS['le']->fetch_assoc())   exit('{"status":"fail","msg":"请先设置销售渠道"}');
		// if($GLOBALS['le']->f('channel')=='') exit('{"status":"fail","msg":"请先设置销售渠道"}');
		$cid = $GLOBALS['le']->f('id');
		// $sql = "update pft_sale_channel set px=$sort where id=$cid limit 1";
		$sql = "update pft_sale_channel set px=$sort where fid=$memberSID and lid=$lid and aid=$aid";
		if($GLOBALS['le']->query($sql)) exit('{"status":"success","msg":"设置成功"}');
		exit('{"status":"fail","msg":"设置失败"}');
		break;
	default:exit('{"status":"fail","msg":"非法操作"}');break;
}



